import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { combineLatest } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
import { MessageService } from 'src/app/core/service';
import { WenyaoReadForm } from 'src/app/shared/components';
import { BillingType, getBillingTypesByValue } from '../model/type';
import { BillingDetail, BillingService } from '../service/billing.service';
import { loadTradeOrderAndMerchantInfo, loadUserTransaction, loadUserWithdrawalCash, LoadUserRecharge } from './utils';
import { NzMessageService } from 'ng-zorro-antd';
import { UserWithdrawalCash } from 'src/app/model';

@Component({
  selector: 'app-billing-detail',
  templateUrl: './billing-detail.component.html',
  styleUrls: ['./billing-detail.component.scss']
})
export class BillingDetailComponent implements OnInit {

  varId: string;
  billingType: BillingType;
  wenyaoReadForms: WenyaoReadForm[];
  data: BillingDetail;
  isBackMSG: boolean = false;

  constructor(
    private msg: MessageService,
    private billingService: BillingService,
    private routes: ActivatedRoute,
    private router: Router,
    private nzMessageService: NzMessageService
  ) {
  }

  cancel() {
    if (this.isUserWithdrawalCash(this.data)) {
      this.billingService.cancelWithdrawalCash(this.data.id).subscribe(res => {
        if (res.status === 0) {
          this.nzMessageService.info('取消成功');
          this.ngOnInit();
        } else {
          this.billingService.error(res);
        }
      }, error => this.billingService.error(error)

      );
    }
  }


  goBack() {
    this.isBackMSG? this.router.navigateByUrl('/member/center/messageListPage') : this.router.navigateByUrl('/billing');
  }

  showCancelButton() {
    if (this.isUserWithdrawalCash(this.data)) {
      return this.data.status === 'PENDING';
    }
  }

  private isUserWithdrawalCash(data: BillingDetail): data is UserWithdrawalCash {
    return 'id' in data && 'status' in data;
  }

  ngOnInit() {
    combineLatest(this.routes.queryParams, this.routes.params)
      .pipe(
        map(res => {
          this.billingType = getBillingTypesByValue(res[0].type);
          this.isBackMSG = res[0]['back'] ;
          this.varId = res[1].id as string;
          return {
            type: this.billingType,
            varId: this.varId,
          };
        }),
        mergeMap(res => this.billingService.getBillling(res)),
        map(res => this.loadWenyaoReadForms(this.billingType, res.data))
      )
      .subscribe(res => {
        this.wenyaoReadForms = res;
      });
  }

  private loadWenyaoReadForms(billingType: BillingType, data: BillingDetail): WenyaoReadForm[] {
    this.data = data;
    // tslint:disable-next-line:no-shadowed-variable
    const map = new Map<string, (data: BillingDetail) => WenyaoReadForm[]>();
    map.set('WITHDRAWAL_CASH', loadUserWithdrawalCash);
    map.set('WITHDRAWAL', loadUserTransaction);
    map.set('DEPOSIT', loadUserTransaction);
    map.set('TRADE_ORDER', loadTradeOrderAndMerchantInfo);
    map.set('RECHARGE', LoadUserRecharge);
    return map.get(billingType.value)(data);
  }
}

